package IO流复习;

import java.io.*;
import java.util.ArrayList;

public class objectOutPutStream {
    public static void main(String[] args) {

        /**
         * 对象序列化流是什么
         * 对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流，从而允许把这种二进制流持久地保存在磁盘上，
         * 或通过网络将这种二进制流传输到另一个网络节点。//当其它程序获取了这种二进制流，就可以恢复成原来的Java对象。
         *
         *
         * 序列化过程:
         * 序列化过程:使用objectoutputstream流实现。将内存中的Java对象保存在文件中或通过网络传输出去
         * 反序列化过程:使用0biectInputSteam流实现。将文件中的数据或网络传输过来的数据还原为内存中的Java对象
         */

        //序列化过程
        student s1 = new student("张三", 18, "男", "移动互联");
        student s2 = new student("张四", 18, "女", "移动互联");
        student s3 = new student("张五", 18, "女", "移动互联");
        ObjectOutputStream op = null;
        try {
            op = new ObjectOutputStream(new FileOutputStream("D:\\idea2022\\day.com.qiezi\\day_13\\src\\IO流复习\\student.txt"));
            op.writeObject(s1);
            op.writeObject(s2);
            op.writeObject(s3);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (op != null)
                    op.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        //反序列化过程
        ObjectInputStream ip = null;
        try {
            ArrayList<String> list=new ArrayList<>();
            ip = new ObjectInputStream(new FileInputStream("D:\\idea2022\\day.com.qiezi\\day_13\\src\\IO流复习\\student.txt"));
            list.add(ip.readObject().toString());
            list.add(ip.readObject().toString());
            list.add(ip.readObject().toString());
            System.out.println(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (ip != null)
                    ip.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class student implements Serializable {
    private String name;
    private int age;
    private String sex;
    private String major;

    public student() {
    }

    public student(String name, int age, String sex, String major) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.major = major;
    }

    /**
     * 获取姓名
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置姓名
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取年纪
     *
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置年纪
     *
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取性别
     *
     * @return sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * 设置性别
     *
     * @param sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * 获取专业
     *
     * @return major
     */
    public String getMajor() {
        return major;
    }

    /**
     * 设置专业
     *
     * @param major
     */
    public void setMajor(String major) {
        this.major = major;
    }

    public String toString() {
        return "student{name = " + name + ", age = " + age + ", sex = " + sex + ", major = " + major + "}";
    }
}
